﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using Atlandiz.Data;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class FormYapilandirma : DevExpress.XtraEditors.XtraForm
    {
        IniAccessLayer ini = new IniAccessLayer(Army.ProgramBaseEnvironmentPath + "\\Tanimlar.ini");
        public bool JustSQL = false;

        public FormYapilandirma()
        {
            InitializeComponent();
        }

        private void Yapilandirma_Load(object sender, EventArgs e)
        {

            Army.EventConfirm = false;
            if (!JustSQL)
            {
                Army.StartupEventsAfterLogin();
                //FirmaHakkinda
                _txtFirmaUnvan.Text = Army.FirmaUnvan;
                _txtFirmaAdres.Text = Army.FirmaAdres;
                _txtFirmaTelefon.Text = Army.FirmaTelefon;
                _txtFirmaEposta.Text = Army.FirmaEposta;
                _txtFirmaYetkiliAdSoyad.Text = Army.FirmaYetkiliAdSoyad;
                _txtFirmaYetkiliUnvan.Text = Army.FirmaYetkiliUnvan;
                //FirmaHakkinda
            }
            else {
                xtraTabControl1.TabPages[1].PageEnabled = false;
                xtraTabControl1.TabPages[2].PageEnabled = false;
                xtraTabControl1.TabPages[3].PageEnabled = false;
                _cbForAllUsers.Enabled = false;
            }
            LoadSettings();
        }

        public void LoadSettings() {

            try
            {
                _txtSqlSunucuString.Text = ini.ReadString("Veritabani", "SqlSunucu");
                _txtSqlVeritabaniString.Text = ini.ReadString("Veritabani", "SqlVeritabani");
                if (_txtSqlVeritabaniString.Text.Length < 1) _txtSqlVeritabaniString.Text = "AtlandizEnvanter";
                _txtSqlKullaniciAdiString.Text = ini.ReadString("Veritabani", "SqlKullaniciAdi");
                _txtSqlParolaString.Text = ini.ReadString("Veritabani", "SqlParola");

                if (ini.ReadBoolean("Veritabani", "UseTrustConnection"))
                {
                    _cbUseTrustConnection.Checked = true;
                }

                if (ini.ReadBoolean("EPosta", "SendEmail"))
                {
                    _cbSendEmail.Checked = true;
                }

                if (ini.ReadBoolean("EPosta", "SslTlsZorunlu"))
                {
                    _cbSslTlsZorunlu.Checked = true;
                }

                _txtSenderName.Text = ini.ReadString("EPosta", "SenderName");
                _txtSenderEmail.Text = ini.ReadString("EPosta", "SenderEmail");
                _txtSmtpServer.Text = ini.ReadString("EPosta", "SmtpServer");
                _txtSmtpPort.Text = ini.ReadString("EPosta", "SmtpPort");
                _txtSmtpUsername.Text = ini.ReadString("EPosta", "SmtpUsername");
                _txtSmtpPassword.Text = ini.ReadString("EPosta", "SmtpPassword");
                _txtRecipientName.Text = ini.ReadString("EPosta", "RecipientName");
                _txtRecipientEmail.Text = ini.ReadString("EPosta", "RecipientEmail");
                _txtMessageHeaderPrefix.Text = ini.ReadString("EPosta", "MessageHeaderPrefix");

                _txtReportForCihaz.Text = ini.ReadString("Raporlar", "ReportForCihaz");
                _txtReportForCihazListe.Text = ini.ReadString("Raporlar", "ReportForCihazListe");
                _txtReportForYazilim.Text = ini.ReadString("Raporlar", "ReportForYazilim");
                _txtReportForYazilimListe.Text = ini.ReadString("Raporlar", "ReportForYazilimListe");
                _txtReportForZimmetGiris.Text = ini.ReadString("Raporlar", "ReportForZimmetGiris");
                _txtReportForZimmetCikis.Text = ini.ReadString("Raporlar", "ReportForZimmetCikis");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hata Mesajı:\n" + ex.Message.ToString(), "Hata Oluştu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }
        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnKaydet_Click(object sender, EventArgs e)
        {
            Army.EventConfirm = true;

            //KAYDET
            ini.Write("Veritabani", "SqlSunucu", _txtSqlSunucuString.Text);
            ini.Write("Veritabani", "SqlVeritabani", _txtSqlVeritabaniString.Text);
            ini.Write("Veritabani", "SqlKullaniciAdi", _txtSqlKullaniciAdiString.Text);
            ini.Write("Veritabani", "SqlParola", _txtSqlParolaString.Text);
            ini.Write("Veritabani", "UseTrustConnection", _cbUseTrustConnection.Checked);

            ini.Write("EPosta", "SendEmail", _cbSendEmail.Checked);
            ini.Write("EPosta", "SslTlsZorunlu", _cbSslTlsZorunlu.Checked);
            ini.Write("EPosta", "SenderName", _txtSenderName.Text);
            ini.Write("EPosta", "SenderEmail", _txtSenderEmail.Text);
            ini.Write("EPosta", "SmtpServer", _txtSmtpServer.Text);
            ini.Write("EPosta", "SmtpPort", _txtSmtpPort.Text);
            ini.Write("EPosta", "SmtpUsername", _txtSmtpUsername.Text);
            ini.Write("EPosta", "SmtpPassword", _txtSmtpPassword.Text);
            ini.Write("EPosta", "RecipientName", _txtRecipientName.Text);
            ini.Write("EPosta", "RecipientEmail", _txtRecipientEmail.Text);
            ini.Write("EPosta", "MessageHeaderPrefix", _txtMessageHeaderPrefix.Text);

            ini.Write("Raporlar", "ReportForCihaz", _txtReportForCihaz.Text);
            ini.Write("Raporlar", "ReportForCihazListe", _txtReportForCihazListe.Text);
            ini.Write("Raporlar", "ReportForYazilim", _txtReportForYazilim.Text);
            ini.Write("Raporlar", "ReportForYazilimListe", _txtReportForYazilimListe.Text);
            ini.Write("Raporlar", "ReportForZimmetGiris", _txtReportForZimmetGiris.Text);
            ini.Write("Raporlar", "ReportForZimmetCikis", _txtReportForZimmetCikis.Text);

            //KAYDET
            Army.StartupEvents();

            if (!JustSQL) {
                //KAYDET for Firma Hakkında
                Army.SetSetting("FirmaUnvan", _txtFirmaUnvan.Text, _cbForAllUsers.Checked);
                Army.SetSetting("FirmaAdres", _txtFirmaAdres.Text, _cbForAllUsers.Checked);
                Army.SetSetting("FirmaTelefon", _txtFirmaTelefon.Text, _cbForAllUsers.Checked);
                Army.SetSetting("FirmaEposta", _txtFirmaEposta.Text, _cbForAllUsers.Checked);
                Army.SetSetting("FirmaYetkiliAdSoyad", _txtFirmaYetkiliAdSoyad.Text, _cbForAllUsers.Checked);
                Army.SetSetting("FirmaYetkiliUnvan", _txtFirmaYetkiliUnvan.Text, _cbForAllUsers.Checked);
                //KAYDET for Firma Hakkında            
                Army.StartupEventsAfterLogin();
            }

            this.Close();
        }

        private void _btnSecForCihaz_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory=Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForCihaz.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally {
                    if (myStream != null) {
                        myStream.Close();
                    }
                }
            }
        }

        private void _btnSecForYazilim_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory = Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForYazilim.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally
                {
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory = Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForZimmetGiris.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally
                {
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory = Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForZimmetCikis.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally
                {
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            FormVeritabaniOlustur vt = new FormVeritabaniOlustur();
            vt.ShowDialog();

            if (Army.EventConfirm) {
                _txtSqlSunucuString.Text = Army.NewVTServer;
                _txtSqlVeritabaniString.Text = Army.NewVTName;
                _txtSqlKullaniciAdiString.Text = Army.NewVTUsername;
                _txtSqlParolaString.Text = Army.NewVTPassword;
                _cbUseTrustConnection.Checked = Army.NewVTTrustConnection;
            }
        }

        public void GetSunucuListesi()
        {

            try
            {
                if (_txtSqlSunucuString.Properties.Items.Count == 0)
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    MsSqlAccessLayer Sql = new MsSqlAccessLayer();
                    DataTable dt = Sql.GetServerList();

                    foreach (DataRow x in dt.Rows)
                    {
                        _txtSqlSunucuString.Properties.Items.Add(x["Name"].ToString());
                    }

                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Hata Oluştu", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void _txtSqlSunucuString_QueryPopUp(object sender, CancelEventArgs e)
        {
            //GetSunucuListesi();
        }

        private void _txtSqlSunucuString_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            //GetSunucuListesi();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory = Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForCihazListe.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally
                {
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            ReportSec.InitialDirectory = Army.ProgramBaseEnvironmentPath + "\\Raporlar";
            ReportSec.FileName = "";
            ReportSec.Filter = "Tasarım 1 Rapor Dosyaları (*.frx)|*.frx|Tasarım 2 Rapor Dosyaları (*.mrt)|*.mrt|Bütün Dosyalar (*.*)|*.*";
            ReportSec.FilterIndex = 1;
            ReportSec.RestoreDirectory = true;

            if (ReportSec.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    myStream = ReportSec.OpenFile();
                    if (myStream != null)
                    {
                        _txtReportForYazilimListe.Text = ReportSec.FileName.Replace(Army.ProgramBaseEnvironmentPath, "");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message.ToString());
                    throw;
                }
                finally
                {
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }


    }
}